package dcc.component;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.table.TableCellEditor;

import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import dcc.mbg.model.MSubtable;
import dcc.service.CommonService;
import dcc.tools.BeanFactory;

public class SubtablePanel extends JPanel {

	private String id;
	private JScrollPane scrollPane;
	private JTable table;
	private String[] headers;
	private String[] fields;
	private List<MSubtable> list;
	private JTableBinding<MSubtable, List<MSubtable>, JTable> jTableBinding;
	private String typeFlag;
	
	public SubtablePanel(String typeFlag) {
		this.typeFlag = typeFlag;
		this.setBorder(new LineBorder(new Color(0, 0, 0)));
		this.setPreferredSize(new Dimension(0, 125));
		this.setLayout(new BorderLayout(0, 0));
		scrollPane = new JScrollPane();
		scrollPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e)) {
					addNewRecord();
				}
			}
		});
		this.add(scrollPane, BorderLayout.CENTER);
		table = createTable();
		scrollPane.setViewportView(table);
	}

	public JTable getTable() {
		return this.table;
	}

	private JTable createTable() {
		JTable table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e)) {
					addNewRecord();
				}
			}
		});

		table.setBackground(Color.WHITE);
		table.setRowHeight(30);
		table.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
		table.getTableHeader().setPreferredSize(new Dimension(0, 30));
		table.getTableHeader().setFont(
				new Font("Microsoft YaHei", Font.PLAIN, 13));
		table.getTableHeader().setBackground(Color.WHITE);
		return table;
	}

	public void setHeader(String[] headers, String[] fields, String id) {
		this.headers = headers;
		this.fields = fields;
		this.id = id;
	}

	public void setData(List<MSubtable> list) {
		this.list = list;
		initDataBindings(this.list);
	}

	private void initDataBindings(List<MSubtable> list) {
		jTableBinding = SwingBindings.createJTableBinding(UpdateStrategy.READ,
				list, this.table, "bindingRole");
		for (int i = 0; i < this.fields.length; i++) {
			BeanProperty<MSubtable, String> f = BeanProperty.create(fields[i]);
			jTableBinding.addColumnBinding(f).setColumnName(this.headers[i])
					.setEditable(true);
		}
		//
		BeanProperty<MSubtable, String> deleteButtonBeanProperty = BeanProperty
				.create(this.id);
		jTableBinding.addColumnBinding(deleteButtonBeanProperty).setColumnName(
				"\u64CD\u4F5C");
		jTableBinding.bind();

		this.table.getColumnModel().getColumn(fields.length).setMaxWidth(40);
		this.table.getColumnModel().getColumn(fields.length)
				.setCellRenderer(new DeleteButtonRenderer());
		this.table.getColumnModel().getColumn(fields.length)
				.setCellEditor(new DeleteButtonEditor(this.table));
	}

	private class DeleteButtonEditor extends AbstractCellEditor implements
			TableCellEditor, ActionListener {


		public DeleteButtonEditor(JTable table) {
			button = new JButton("\u5220\u9664");
			button.addActionListener(this);
		}

		private JButton button;

		@Override
		public Object getCellEditorValue() {
			return button.getActionCommand();
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			deleteRow(button.getActionCommand());
			fireEditingStopped();
		}

		@Override
		public Component getTableCellEditorComponent(JTable table,
				Object value, boolean isSelected, int row, int column) {
			this.button.setActionCommand("" + value + "&" + row);
			return button;
		}
	}

	public void addNewRecord() {
		MSubtable item = new MSubtable();
		item.setTypeFlag(this.typeFlag);
		jTableBinding.unbind();
		this.list.add(item);
		initDataBindings(this.list);
	}

	public void deleteRow(String actionCommand) {
		String[] commands = actionCommand.split("&");
		if (!commands[0].equals("null")){
			((CommonService) BeanFactory.getBean("commonService"))
			.deleteMSubtable(Integer.parseInt(commands[0]));
		}
		deleteItemInList(Integer.parseInt(commands[1]));
		update();
	}
	
	private void deleteItemInList(int row){
		list.remove(row);
	}

	public void update() {
		jTableBinding.unbind();
		initDataBindings(this.list);
	}
}
